package com.hs.controller;

import com.github.pagehelper.PageInfo;
import com.hs.entity.RestBean;
import com.hs.entity.dto.Order;
import com.hs.service.OrderService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/order")
public class OrderController {
    @Resource
    private OrderService orderService;

    @GetMapping("/getOrderList/{currentNum}")
    public RestBean<PageInfo<Order>> getOrderList(
            @PathVariable("currentNum") Integer currentNum,
            @RequestParam(required = false) String keywords) {
        PageInfo<Order> orderList = orderService.selectOrderList(currentNum,keywords);
        return RestBean.success(orderList);
    }

    @GetMapping("/getAllOrderList")
    public RestBean<List<Order>> getAllOrderList(){
        List<Order> orderList = orderService.selectAllOrderList();
        return RestBean.success(orderList);
    }

    @GetMapping("/getOrderListByUser/{currentNum}")
    public RestBean<PageInfo<Order>> getOrderListByUser(
            @PathVariable("currentNum") Integer currentNum,
            @RequestParam(required = false) String keywords,
            @RequestParam Integer userId) {
        // 添加用户ID条件
//        log.info(String.valueOf(userId));
        PageInfo<Order> orderList = orderService.selectOrderListByUser(currentNum, keywords, userId);
        return RestBean.success(orderList);
    }

    @GetMapping("/getOrderListByTeamId/{currentNum}")
    public RestBean<PageInfo<Order>> getOrderListByTeamId(
            @PathVariable("currentNum") Integer currentNum,
            @RequestParam(required = false) String keywords,
            @RequestParam Integer teamId) {
        // 添加用户ID条件
//        log.info(String.valueOf(userId));
        PageInfo<Order> orderList = orderService.selectOrderListByTeamId(currentNum, keywords, teamId);
        return RestBean.success(orderList);
    }


    @GetMapping("/getAllOrderListByTeamId/{teamId}")
    public RestBean<List<Order>> getAllOrderListByTeamId(
            @PathVariable  Integer teamId) {
        // 添加用户ID条件
//        log.info(String.valueOf(userId));
        List<Order> orderList = orderService.selectAllOrderListByTeamId(teamId);
        return RestBean.success(orderList);
    }

    @PostMapping("/updateOrder")
    public RestBean<Order> updateOrder( @RequestBody Order order) {
        System.out.println(order);
        orderService.updateById(order);
        return RestBean.success(null);
    }

    @PostMapping("/addOrder")
    public RestBean<Order> addOrder( @RequestBody Order order) {
        order.setCreate_time(LocalDateTime.now());
        orderService.save(order);
        return RestBean.success(null);
    }

    @PostMapping("/deleteOrder/{id}")
    public RestBean<Order> deleteOrder(@PathVariable  Integer id) {
        orderService.removeById(id);
        return RestBean.success(null);
    }

    @PostMapping("/acceptOrder/{id}")
    public RestBean<Void> acceptOrder(@PathVariable Integer id) {
        orderService.updateOrderStatus(id,1);
        return RestBean.success(null);
    }

    @PostMapping("/rejectOrder/{id}")
    public RestBean<Void> rejectOrder(@PathVariable Integer id) {
        orderService.updateOrderStatus(id,4);
        return RestBean.success(null);
    }

    @PostMapping("/completeService/{id}")
    public RestBean<Void> completeService(@PathVariable Integer id) {
        orderService.updateOrderStatus(id,2);
        return RestBean.success(null);
    }
}
